<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "pay_payment_channel".
 *
 * @property int $cid
 * @property string $channel_id 渠道id
 * @property int $type 1-微信支付，2-支付宝，3-银联
 * @property int $scenario 1-移动app，2-移动网页，3-PC网页，4-微信公众平台，5手机扫码
 * @property string $status 1-启用，2-禁用
 * @property string|null $remake 备注
 * @property string|null $channel_parameter 渠道参数
 * @property string|null $create_time 创建时间
 */
class PaymentChannel extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'pay_payment_channel';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['channel_id', 'type', 'scenario'], 'required'],
            [['type', 'scenario'], 'integer'],
            [['status'], 'string'],
            [['create_time'], 'safe'],
            [['channel_id'], 'string', 'max' => 10],
            [['remake'], 'string', 'max' => 255],
            [['channel_parameter'], 'string', 'max' => 512],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'cid' => 'Cid',
            'channel_id' => 'Channel ID',
            'type' => 'Type',
            'scenario' => 'Scenario',
            'status' => 'Status',
            'remake' => 'Remake',
            'channel_parameter' => 'Channel Parameter',
            'create_time' => 'Create Time',
        ];
    }


    /*** 获取支付渠道详情
     * @param $cid
     * @return array|null|\yii\db\ActiveRecord
     */
    public function gainInfo($cid)
    {
        $info = self::find()->where('cid = :id',[':id' => $cid])->asArray()->one();
        if (!empty($info)){
            $info['parameter'] = json_decode($info['channel_parameter'],true);
            unset($info['channel_parameter']);
            return $info;
        }
        return [];
    }
}
